package com.goodjob.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.goodjob.musicplayer.R;

public class EditActivity extends AppCompatActivity {
    private static final String SHARED_PREFERENCES_NAME = "music_player";
    private ImageView returnImg;
    private EditText username_Text;
    private EditText userNick_Text;
    private EditText userPhone_Text;
    private EditText userEmail_Text;
    private Button subBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        returnImg = (ImageView)findViewById(R.id.button_return1) ;
        subBtn = (Button) findViewById(R.id.btn_modify);
        userEmail_Text = (EditText) findViewById(R.id.text_userEmail);
        username_Text = (EditText) findViewById(R.id.text_username);
        userNick_Text = (EditText) findViewById(R.id.text_userNick);
        userPhone_Text = (EditText) findViewById(R.id.text_userPhone);
        username_Text.setEnabled(false);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EditActivity.this,ListActivity.class);
                startActivity(intent);
            }
        });

        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("is", MODE_PRIVATE);
                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = sharedPreferences.edit();

               // editor.putString("name", String.valueOf(username_Text.getText()));
                editor.putString("nick",String.valueOf(userNick_Text.getText()));
                editor.putString("phone",String.valueOf(userPhone_Text.getText()));
                editor.putString("email",String.valueOf(userEmail_Text.getText()));
                editor.apply();
                Toast.makeText(EditActivity.this,"修改成功！",Toast.LENGTH_LONG).show();
                finish();
            }
        });
        initView();

    }

    void initView(){
        SharedPreferences sharedPreferences = getSharedPreferences("is", MODE_PRIVATE);
        try{
            username_Text.setText(sharedPreferences.getString("name",""));

            if (sharedPreferences.getString("phone", "").equals("")){
                userPhone_Text.setText("暂未设置");
            }else{
                userPhone_Text.setText(sharedPreferences.getString("phone", ""));
            }

            if (sharedPreferences.getString("nick", "").equals("")){
                userNick_Text.setText("暂未设置");
            }else{
                userNick_Text.setText(sharedPreferences.getString("nick",""));
            }

            if (sharedPreferences.getString("email", "").equals("")){
                userEmail_Text.setText("暂未设置");
            }else{
                userEmail_Text.setText(sharedPreferences.getString("email",""));
            }

        }catch (Exception e){

        }

    }
}
